<template>
  <div class="admin_page">
    <admin-header></admin-header>
    <admin-side-bar></admin-side-bar>
    <admin-container>
      <el-form inline size="mini" :model="searchForm">
        <el-form-item>问卷 的题目</el-form-item>
        <el-form-item>
          <el-input
            v-model="searchForm.name"
            prefix-icon="el-icon-search"
            placeholder="name or key"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getData">
            搜索
          </el-button>
          <el-button type="primary" @click="() => $refs.add.show()">
            添加
          </el-button>
          <el-button
            type="primary"
            @click="
              () =>
                $refs.cache.show({
                  survey_id: $route.query.survey_id
                })
            "
          >
            更新数据
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="appLoading"
        border
        size="mini"
        :data="data.data"
        default-expand-all
      >
        <el-table-column type="expand">
          <template slot-scope="{ row }">
            <el-row v-for="(item, idx) in row.options" :key="idx">
              <el-col :span="16" class="fs12">
                {{ idx + 1 }}. {{ item.name }}
              </el-col>
              <el-col :span="8">
                <el-button
                  type="text"
                  size="mini"
                  @click="() => $refs.addOption.show(item)"
                >
                  修改
                </el-button>
                <el-button type="text" size="mini" @click="delOption(item)"
                  >删除</el-button
                >
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="ID" prop="id" width="60px"></el-table-column>
        <el-table-column label="标题" prop="name"></el-table-column>
        <el-table-column label="题目类型">
          <template slot-scope="{ row }">
            {{ row.type | questionTypeFilter }}
          </template>
        </el-table-column>
        <el-table-column label="选项数">
          <template slot-scope="{ row }">
            {{ row.options.length }}
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column fixed="right" label="操作" width="300px">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="mini"
              @click="
                $refs.cache.show({
                  id: row.id
                })
              "
            >
              更新数据
            </el-button>
            <el-button
              v-if="row.type === 1 || row.type === 2"
              type="text"
              size="mini"
              @click="
                () =>
                  $refs.addOption.show({
                    question_id: row.id
                  })
              "
            >
              选项添加
            </el-button>
            <el-button type="text" size="mini" @click="$refs.add.show(row)">
              修改
            </el-button>
            <el-button type="text" size="mini" @click="del(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt10 bg_fff">
        <el-pagination
          :total="page.total"
          :page-size="page.pageSize"
          :current-page="page.pageNum"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </admin-container>
    <survey-question-add-dialog
      ref="add"
      @ok="id => $refs.cache.show({ id })"
    ></survey-question-add-dialog>
    <survey-question-cache-update-dialog
      ref="cache"
      @ok="refresh"
    ></survey-question-cache-update-dialog>
    <survey-option-add-dialog
      ref="addOption"
      @ok="id => $refs.cacheOption.show({ id })"
    ></survey-option-add-dialog>
    <survey-option-cache-update-dialog
      ref="cacheOption"
      @ok="refresh"
    ></survey-option-cache-update-dialog>
  </div>
</template>

<script>
import AdminHeader from "../../../components/admin/header";
import AdminSideBar from "../../../components/admin/side-bar";
import AdminContainer from "../../../components/admin/container";
import { AppLoadingMixin } from "../../../lib/mixins/app-loading";
import { PageMixin } from "../../../lib/mixins/PageMixin";
import SurveyQuestionAddDialog from "../../../components/survey/question-add-dialog";
import SurveyQuestionCacheUpdateDialog from "../../../components/survey/question-cache-dialog";
import { questionTypeFilter } from "../../../lib/filters/survey";
import SurveyOptionAddDialog from "../../../components/survey/option-add-dialog";
import SurveyOptionCacheUpdateDialog from "../../../components/survey/option-cache-dialog";

export default {
  name: "QuestionList",
  filters: { questionTypeFilter },
  components: {
    SurveyOptionCacheUpdateDialog,
    SurveyOptionAddDialog,
    SurveyQuestionCacheUpdateDialog,
    SurveyQuestionAddDialog,
    AdminContainer,
    AdminSideBar,
    AdminHeader
  },
  mixins: [AppLoadingMixin, PageMixin],
  data() {
    return {
      form: {},
      searchForm: { survey_id: "", order: "sort asc,id asc" }
    };
  },
  computed: {},
  mounted() {
    this.searchForm.survey_id = this.$route.query.survey_id;
    this.getData();
  },
  methods: {
    del(data) {
      this.$confirm(`是否删除${data.name}?`).then(async () => {
        if (await this.$surveyService.questionDel(data.id + "")) {
          this.calculatePage("-");
          this.getData();
        }
      });
    },
    delOption(data) {
      this.$confirm(`是否删除${data.name}?`).then(async () => {
        if (await this.$surveyService.optionDel(data.id + "")) {
          this.getData();
        }
      });
    },
    async getData() {
      const { page, searchForm } = this;
      this.data.data.splice(0);
      this.data = await this.$surveyService.questionList({
        ...page,
        ...searchForm
      });
      this.data.data.map(r => {
        r.hasChildren = !!(r.type === 1 || r.type === 2);
      });
    }
  }
};
</script>

<style scoped></style>
